﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace NG
{
    class CriteriaHandler:ICriteriaHandler
    {

        private string tableName = "[dbo].[Criteria]";
        public void addCriteria(Criteria cri)
        {

            DBConnect con = new DBConnect();
            string tbn = "[" + con.dbName + "]." + this.tableName;
            if (con.OpenConnection() == false)
            {
                throw new System.Exception("Couldn't connect to the Database");
            }
            string query = string.Format("INSERT INTO" + tbn + "(questionNo,criteriaDesc,isObservable)VALUES("
                + "{0},'{1}','{2}');", cri.QuestionNum,cri.CriteriaDesc,cri.isObservable);


            SqlCommand cmd = new SqlCommand(query, con.connection);
            cmd.ExecuteNonQuery();
            if (con.CloseConnection() == false)
            {
                throw new System.Exception("Couldn't disconnect from the Database");
            }
        }

        public void updateCriteria(Criteria cri)
        {
            DBConnect con = new DBConnect();
            string tbn = "[" + con.dbName + "]." + this.tableName;
            if (con.OpenConnection() == false)
            {
                throw new System.Exception("Couldn't connect to the Database");
            }
            string query = string.Format("UPDATE" + tbn + " SET questionNo= {0}, criteriaDesc= '{1}' , isObservable = {2}"
                + "WHERE  criteriaNo = {3} ", cri.QuestionNum,cri.CriteriaDesc,cri.isObservable,cri.CriteriaNum);
            SqlCommand cmd = new SqlCommand(query, con.connection);
            cmd.ExecuteNonQuery();
            if (con.CloseConnection() == false)
            {
                throw new System.Exception("Couldn't disconnect from the Database");
            }
        }

        public void deleteCriteria(Criteria cri)
        {
            DBConnect con = new DBConnect();
            string tbn = "[" + con.dbName + "]." + this.tableName;

            if (con.OpenConnection() == false)
            {
                throw new System.Exception("Couldn't connect to the Database");
            }

            string query = string.Format("DELETE FROM " + tbn + " WHERE criteriaNo = {0}  ", cri.CriteriaNum);
            SqlCommand cmd = new SqlCommand(query, con.connection);
            cmd.ExecuteNonQuery();

            if (con.CloseConnection() == false)
            {
                throw new System.Exception("Couldn't disconnect from the Database");
            }
        }

        public LinkedList<Criteria> allCriterias(string sortedBy, bool desc)
        {
            string sorted;
            if (sortedBy == string.Empty)
            {
                sorted = " {0}";
            }
            else
            {
                sorted = sortedBy;
            }
            DBConnect con = new DBConnect();
            string tbn = "[" + con.dbName + "]." + this.tableName;
            LinkedList<Criteria> criteriaList = new LinkedList<Criteria>();
            if (con.OpenConnection() == false)
            {
                throw new System.Exception("Couldn't connect to the Database");
            }
            string extra = "";
            if (desc)
            {
                extra = " DESC";
            }

            string query = "SELECT * FROM " + tbn + "ORDER BY " + sorted + extra;
            SqlCommand cmd = new SqlCommand(query, con.connection);
            SqlDataReader rdr = cmd.ExecuteReader();


            if (rdr.HasRows)
            {

                while (rdr.Read())
                {

                    Criteria inter = new Criteria(int.Parse(rdr["criteriaNo"].ToString()),
                            int.Parse(rdr["questionNo"].ToString()), rdr["criteriaDesc"].ToString(), bool.Parse(rdr["isObservable"].ToString()));


                    criteriaList.AddLast(inter);


                }
            }
            if (con.CloseConnection() == false)
            {
                throw new System.Exception("Couldn't disconnect from the Database");
            }
            return criteriaList;
        }

    }

        
    }

